﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Core.GenericDefinitions;
using Core.GenericDefinitions.BaseClasses;
using Core.Requests.SRVUICommonRequests;

namespace UI.Common
{
    public class MainPageViewModel : ViewModelBase
    {
        public MainPageViewModel()
        {
            LoginCommand = new ViewModelCommand(LoginCommandExecute, LoginCommandCanExecute);
            LogoutCommand = new ViewModelCommand(LogoutCommandExecute, LogoutCommandCanExecute);
        }

        #region CurrentUser VM Property
        public User CurrentUser
        {
            get
            {
                if (IsInDesignMode)
                {
                    return new User { Name = "Mehmet Fatih", Surname = "AKAN", UserName = "fakan" };
                }
                return ApplicationInformation.CurrentUser;
            }
            set
            {
                if (ApplicationInformation.CurrentUser == value) return;
                ApplicationInformation.CurrentUser = value;
                RaisePropertyChanged("CurrentUser");
                RaisePropertyChanged("WellcomeText");
            }
        }
        #endregion CurrentUser

        #region LoginInfo VM Property
        public LoginInformation LoginInfo
        {
            get
            {
                // if (ViewModelBase.IsInDesignModeStatic)
                // {                
                // }
                return ApplicationInformation.LoginInfo;
            }
            set
            {
                if (ApplicationInformation.LoginInfo == value) return;
                ApplicationInformation.LoginInfo = value;
                RaisePropertyChanged("LoginInfo");
            }
        }
        #endregion LoginInfo

        #region WellcomeText VM Property
        public string WellcomeText
        {
            get
            {
                if (IsInDesignMode)
                {
                    return "Lütfen giriş yapınız";
                }

                if (ApplicationInformation.CurrentUser.IsLoggedIn)
                {
                    return String.Format("Hoşgeldiniz {0}", ApplicationInformation.CurrentUser.Name);
                }
                else
                {
                    return "Lütfen giriş yapınız";
                }
            }
            set
            {
                RaisePropertyChanged("WellcomeText");
            }
        }
        #endregion WellcomeText

        #region LoginCommand
        //LoginCommand = new ViewModelCommand(LoginCommandExecute, LoginCommandCanExecute);
        public ViewModelCommand LoginCommand { get; set; }
        private void LoginCommandExecute()
        {
            LoginRequest lgnReq = new LoginRequest();
            App.Transporter.Send(lgnReq,
                (response) =>
                {
                    if (response.Error != null)
                    {
                        MessageBox.Show("Hata: " + response.Message);
                    }
                    else
                    {
                        CurrentUser = (User)response.ReturnValue;
                        LogoutCommand.RaiseCanExecuteChanged();
                        LoginCommand.RaiseCanExecuteChanged();
                        RaisePropertyChanged("LoginPanelVisibility");
                        RaisePropertyChanged("LogoutButtonVisibility");
                    }

                }
                );
        }
        private bool LoginCommandCanExecute()
        {
            if (ViewModelBase.IsInDesignModeStatic)
            {
                return true;
            }
            else
            {
                return !CurrentUser.IsLoggedIn
                    // && LoginInfo.UserName != "" 
                    // && LoginInfo.Password != ""
                  ;
            }
        }
        #endregion LoginCommand

        #region LogoutCommand
        //LogoutCommand = new ViewModelCommand(LogoutCommandExecute,LogoutCommandCanExecute);
        /// <summary>
        /// Çıkış yapmaya yarayan command
        /// </summary>
        public ViewModelCommand LogoutCommand { get; set; }
        private void LogoutCommandExecute()
        {
            LogoutRequest request = new LogoutRequest();
           
            App.Transporter.Send(request,
                (response) =>
                {
                    if (response.Error != null)
                    {
                        Operations.WarnUser("Hata: " + response.Message);
                    }
                    else
                    {
                        CurrentUser = new User();
                        LoginInfo = new LoginInformation();
                        LogoutCommand.RaiseCanExecuteChanged();
                        LoginCommand.RaiseCanExecuteChanged();
                        RaisePropertyChanged("LoginPanelVisibility");
                        RaisePropertyChanged("LogoutButtonVisibility");
                    }
                }
                );
        }
        private bool LogoutCommandCanExecute()
        {
            if (ViewModelBase.IsInDesignModeStatic)
            {
                return true;
            }
            else
            {
                return CurrentUser.IsLoggedIn;
            }
        }
        #endregion LogoutCommand

        #region LoginPanelVisibility VM Property        
        public Visibility LoginPanelVisibility
        {
            get
            {
                if (ViewModelBase.IsInDesignModeStatic)
                {
                    return Visibility.Visible;
                }
                if (CurrentUser.IsLoggedIn)
                {
                    return Visibility.Collapsed;
                }
                else
                {
                    return Visibility.Visible;
                }
            }

        }
        #endregion LoginPanelVisibility

        #region LogoutButtonVisibility VM Property
        public Visibility LogoutButtonVisibility
        {
            get
            {
                if (ViewModelBase.IsInDesignModeStatic)
                {
                    return Visibility.Visible;
                }
                if (CurrentUser.IsLoggedIn)
                {
                    return Visibility.Visible;
                }
                else
                {
                    return Visibility.Collapsed;
                }
            }
        }
        #endregion LogoutButtonVisibility

        #region IsApplicationBussy VM Property
        public bool IsApplicationBussy
        {
            get
            {
                if (ViewModelBase.IsInDesignModeStatic)
                {
                    return false;
                }
                return App.Transporter.IsBussy;
            }
            set
            {
                App.Transporter.IsBussy = value;
                RaisePropertyChanged("IsApplicationBussy");
            }
        }
        #endregion IsApplicationBussy

    }
}
